package jp.sourceforge.akjrcp.snack;

import java.util.ArrayList;
import java.util.List;

public class ValueChangingTool {

	private List<Changing> averagings=new ArrayList<Changing>();
	private String sepalator=" "; //connma make problen when save param Systems
	public ValueChangingTool(int max,String text){
		String[] words=text.split(sepalator);
		for (int i = 0; i < words.length; i++) {
			if(words[i].startsWith("+")){
				int p=0;
				try{
					p=Integer.parseInt(words[i].substring(1));
				}catch(Exception e){
					e.printStackTrace();
				}
				if(p!=0){
					averagings.add(new Plus(max,p));
				}
			}else if(words[i].endsWith("%")){
				int p=0;
				try{
					p=Integer.parseInt(words[i].substring(0,words[i].length()-1));
				}catch(Exception e){
					e.printStackTrace();
				}
				if(p!=0){
					double pd=(double)p/100;
					averagings.add(new Compresser((int)(pd*max)));
				}
			}else 	if(words[i].startsWith("x")){
				double p=0;
				try{
					p=Double.parseDouble(words[i].substring(1));
				}catch(Exception e){
					e.printStackTrace();
				}
				if(p!=0){
					averagings.add(new Multiply(max,p));
				}
			}
		}
	}
	public int[] change(int values[]){
		int[] ret=null;
		for(int i=0;i<averagings.size();i++){
			int arg[];
			if(ret==null){
				arg=values;
			}else{
				arg=ret;
			}
			ret=averagings.get(i).changing(arg, null);
		}
		return ret;
	}
	public interface Changing{
		public int[] changing(int[] values,Object option);
	}
	
	
	public class Compresser implements Changing{
		private int max;
		public Compresser(int max){
			this.max=max;
		}
		@Override
		public int[] changing(int[] values, Object option) {
			int searchPeek=0;
			int ret[]=new int[values.length];
			for (int i = 0; i < ret.length; i++) {
				if(values[i]>searchPeek){
					searchPeek=values[i];
				}
			}
			
			
			double ratio=(double)max/searchPeek;
			//log.trace("max:"+max+",peak:"+searchPeek+" ratio="+ratio);
			for (int i = 0; i < ret.length; i++) {
			ret[i]=(int)(ratio*values[i]);
			}
			return ret;
		}
		
	}
	
	public class Plus implements Changing{
		private int plus;
		private int max;
		public Plus(int max,int plus){
			this.plus=plus;
			this.max=max;
		}
		@Override
		public int[] changing(int[] values, Object option) {
			int ret[]=new int[values.length];
			
		
			for (int i = 0; i < ret.length; i++) {
			ret[i]=Math.min(max,values[i]+plus);
			}
			return ret;
		}
	}
	
	public class Multiply implements Changing{
		private double multiply;
		private int max;
		public Multiply(int max,double plus){
			this.multiply=plus;
			this.max=max;
		}
		@Override
		public int[] changing(int[] values, Object option) {
			int ret[]=new int[values.length];
			
		
			for (int i = 0; i < ret.length; i++) {
			ret[i]=Math.min(max,(int)(values[i]*multiply));
			}
			return ret;
		}
	}

	public String getSepalator() {
		return sepalator;
	}
	public void setSepalator(String sepalator) {
		this.sepalator = sepalator;
	}
}
